package com.cskaoyan.service.impl;

import com.cskaoyan.bean.bo.FootListBo;
import com.cskaoyan.bean.pojo.MarketFootprint;
import com.cskaoyan.bean.pojo.MarketFootprintExample;
import com.cskaoyan.bean.pojo.MarketGoods;
import com.cskaoyan.bean.pojo.MarketUser;
import com.cskaoyan.bean.vo.FootListVO;
import com.cskaoyan.mapper.MarketFootprintMapper;
import com.cskaoyan.mapper.MarketGoodsMapper;
import com.cskaoyan.service.FootPrintService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author :LuoXuXin
 * @version : $version
 * @date :Created in 2022/4/8   23:10
 * @description:
 * @return:
 */
@Service
public class FootPrintServiceImpl implements FootPrintService {
    @Autowired
    MarketFootprintMapper marketFootprintMapper;
    @Autowired
    MarketGoodsMapper marketGoodsMapper;


    @Transactional
    @Override
    public FootListVO queryAllFoot(FootListBo footListBo) {
        Integer limit = footListBo.getLimit();
        Integer page = footListBo.getPage();
        FootListVO footListVO = new FootListVO();
        List<FootListVO.ListInner> listInners = new ArrayList<>();
        //开启分页
        PageHelper.startPage(page, limit);
        //查询历史足迹表中有的数据(本登录账号的）
        Subject subject = SecurityUtils.getSubject();
        MarketUser marketUser= ((MarketUser) subject.getPrincipal());
        MarketFootprintExample marketFootprintExample = new MarketFootprintExample();
        MarketFootprintExample.Criteria criteria = marketFootprintExample.createCriteria();
        //仅展示状态为未删除的
        criteria.andDeletedEqualTo(false);
        //仅展示登录账号本人的足迹
        criteria.andUserIdEqualTo(marketUser.getId());
        //历史足迹  时间最近的排在前面
        marketFootprintExample.setOrderByClause("add_time desc");
        //按以上条件查询
        List<MarketFootprint> marketFootprints = marketFootprintMapper.selectByExample(marketFootprintExample);
        PageInfo<MarketFootprint> footprintPageInfo = new PageInfo<>(marketFootprints);
        int pages = footprintPageInfo.getPages();
        int total = (int) footprintPageInfo.getTotal();
        footListVO.setLimit(limit);
        footListVO.setPage(page);
        footListVO.setPages(pages);
        footListVO.setTotal(total);
        for (MarketFootprint marketFootprint : marketFootprints) {
            FootListVO.ListInner listInner = new FootListVO.ListInner();
            listInner.setId(marketFootprint.getId());
            listInner.setGoodsId(marketFootprint.getGoodsId());
            listInner.setAddTime(marketFootprint.getAddTime().toString());
            listInners.add(listInner);
        }
        //查询商品表中的数据
        for (FootListVO.ListInner listInner : listInners) {
            Integer goodsId = listInner.getGoodsId();
            MarketGoods marketGoods =
                    marketGoodsMapper.selectByPrimaryKey(goodsId);
            listInner.setBrief(marketGoods.getBrief());
            listInner.setPicUrl(marketGoods.getPicUrl());
            listInner.setName(marketGoods.getName());
            listInner.setRetailPrice((marketGoods.getRetailPrice()));

        }
        footListVO.setList(listInners);
        return footListVO;
    }

    @Override
    public void deleteFoot(MarketFootprint marketFootprint) {
        MarketFootprint marketFootprint1 = marketFootprintMapper.selectByPrimaryKey(marketFootprint.getId());
        marketFootprint1.setDeleted(true);
        marketFootprintMapper.updateByPrimaryKey(marketFootprint1);
    }
}
